package online.heycm.result;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * 接口统一返回
 * @author heycm
 * @since 2021-03-25 11:46:02
 */
@Data
@Builder
@SuppressWarnings({"unchecked"})
public class ResultVO<T> implements Serializable {

    private Boolean ok;

    private Integer code;

    private String message;

    private T data;

    public static <T> ResultVO<T> ok() {
        return (ResultVO<T>) ResultVO.builder().ok(true).build();
    }

    public static <T> ResultVO<T> ok(Integer code) {
        return (ResultVO<T>) ResultVO.builder().ok(true).code(code).build();
    }

    public static <T> ResultVO<T> ok(String message) {
        return (ResultVO<T>) ResultVO.builder().ok(true).message(message).build();
    }

    public static <T> ResultVO<T> ok(Integer code, String message) {
        ResultVOBuilder<Object> builder = ResultVO.builder();
        return (ResultVO<T>) ResultVO.builder().ok(true).code(code).message(message).build();
    }

    public static <T> ResultVO<T> data(T data) {
        return (ResultVO<T>) ResultVO.builder().ok(true).data(data).build();
    }

    public static <T> ResultVO<T> data(String message, T data) {
        return (ResultVO<T>) ResultVO.builder().ok(true).message(message).data(data).build();
    }

    public static <T> ResultVO<T> data(Integer code, T data) {
        return (ResultVO<T>) ResultVO.builder().ok(true).code(code).data(data).build();
    }

    public static <T> ResultVO<T> data(Integer code, String message, T data) {
        return (ResultVO<T>) ResultVO.builder().ok(true).code(code).message(message).data(data).build();
    }

    public static <T> ResultVO<T> error(String message) {
        return (ResultVO<T>) ResultVO.builder().ok(false).message(message).build();
    }

    public static <T> ResultVO<T> error(Integer code) {
        return (ResultVO<T>) ResultVO.builder().ok(false).code(code).build();
    }

    public static <T> ResultVO<T> error(String message, T data) {
        return (ResultVO<T>) ResultVO.builder().ok(false).message(message).data(data).build();
    }

    public static <T> ResultVO<T> error(Integer code, T data) {
        return (ResultVO<T>) ResultVO.builder().ok(false).code(code).data(data).build();
    }

    public static <T> ResultVO<T> error(Integer code, String message, T data) {
        return (ResultVO<T>) ResultVO.builder().ok(false).code(code).message(message).data(data).build();
    }

    public static <T> ResultVO<T> oe(boolean ok, String okMsg, String errMsg) {
        return (ResultVO<T>) ResultVO.builder().ok(ok).message(ok ? okMsg : errMsg).build();
    }
}
